package formin.powerbar.gui;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.BoxLayout;

import formin.powerbar.core.PowerBarGUI;
import javax.swing.Box;

public class SliderPanel extends JPanel {
	
	private PowerBarGUI guiRef;
	
	
	private JPanel leftPanel;
	private JPanel rightPanel;
	private JPanel slidePanel;
	
	
	private JSlider startSlider;
	private JSlider endSlider;
	private JLabel startLabel;
	private JLabel endLabel;
	private SliderListener sliderListener;
	
	
	private JButton onButton;
	private JButton offButton;
	private ButtonListener buttonListener;

	//SlidePanel constructor
	public SliderPanel(PowerBarGUI gui){
		//reference to gui for method calls
		guiRef = gui;
		
		//create layout managers and panels for gui components
		this.setLayout(new BoxLayout(this,BoxLayout.X_AXIS));
		
		leftPanel = new JPanel();
		leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.Y_AXIS));
		
		rightPanel = new JPanel();
		rightPanel.setLayout(new BoxLayout(rightPanel, BoxLayout.X_AXIS));
		
		//slidePanel contains the 2 sliders
		slidePanel = new JPanel();
		slidePanel.setLayout(new BoxLayout(slidePanel, BoxLayout.Y_AXIS));
		startSlider =  new JSlider(JSlider.HORIZONTAL,0,95,0);
		endSlider =  new JSlider(JSlider.HORIZONTAL,0,95,95);
		sliderListener = new SliderListener();
		startSlider.addChangeListener(sliderListener);
		endSlider.addChangeListener(sliderListener);
		
		//labels that display the value of the sliders
		startLabel = new JLabel("0");
		endLabel = new JLabel("95");
		
		//buttons
		buttonListener = new ButtonListener();
		onButton = new JButton("On");
		offButton = new JButton("Off");
		onButton.addActionListener(buttonListener);
		offButton.addActionListener(buttonListener);
		
		rightPanel.add(onButton);
		rightPanel.add(offButton);
		rightPanel.add(startLabel);
		rightPanel.add(endLabel);
		
		
		slidePanel.add(startSlider);
		slidePanel.add(endSlider);
		
		leftPanel.add(slidePanel);
		
		this.add(leftPanel);
		this.add(rightPanel);
	

		
	}
	
	public int getStartSliderValue(){
		return startSlider.getValue();
	}
	
	public int getEndSliderValue(){
		return endSlider.getValue();
	}
	
	
	private class SliderListener implements ChangeListener {
	    public void stateChanged(ChangeEvent e) {
	    	 JSlider jSlider = (JSlider)e.getSource();
	    	 String timeValue = String.valueOf(jSlider.getValue());
	    	 if (jSlider == startSlider){
	    		 startLabel.setText(timeValue);  
	    	 }else if (jSlider == endSlider){
	    		 endLabel.setText(timeValue);
	    	 }
	    }
	}
	
	private class ButtonListener implements ActionListener{
		public void actionPerformed(ActionEvent e){
		      String text = (String)e.getActionCommand();
		      
		      if(text == "On"){
		    	 guiRef.editOnOffCycle(startSlider.getValue(), endSlider.getValue(), true);
				
		      }else if (text == "Off"){
		    	 // turn off
		    	  guiRef.editOnOffCycle(startSlider.getValue(), endSlider.getValue(), false);
		      }
		}
	}

}
